﻿using System;
using System.ComponentModel;
using System.Web.Mvc;
using viBook.Common;
using viBook.Controllers;

namespace viBook.ModelBinders
{
    public class BookBinders : DefaultModelBinder
    {
        protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
        {
            if (propertyDescriptor.Name == "UploadedDate")
            {
                propertyDescriptor.SetValue(bindingContext.Model, DateTime.Now);
                return;
            }
            if (propertyDescriptor.Name == "UploadedBy")
            {
                propertyDescriptor.SetValue(bindingContext.Model, ((BaseController) controllerContext.Controller).ExtendedIdentity.Id);
                return;
            }

            if (propertyDescriptor.Name == "CoverUrl")
            {
                string coverUrl = !string.IsNullOrEmpty(controllerContext.HttpContext.Request["filesUploaded"])
                                      ? controllerContext.HttpContext.Request["filesUploaded"]
                                      : Constant.DefaultBookUrl;
                propertyDescriptor.SetValue(bindingContext.Model, coverUrl);
                return;
            }
            base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
        }
    }
}